package imu.ming.spring.controller;

import imu.ming.spring.annotation.SystemLog;
import imu.ming.spring.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping(value = "login")
    public String  login(){
        return "login";
    }

    @PostMapping(value = "login")
    @SystemLog(module = "系统操作", content = "用户登录")
    public String doLogin(String  id, String pw, ModelMap modelMap,HttpServletRequest request,  HttpSession session) throws IOException {
       boolean isSuccess= userService.doLogin(id,pw);
        if(isSuccess){
            modelMap.addAttribute("name",id);
//            request.sendRedirect("/code_war_exploded/main");
            request.setAttribute("msg","登录成功");

            return "main";
        }
        else {
//            request.sendRedirect("/code_war_exploded/login");
            request.setAttribute("msg","登录失败");
            return "login";
        }


    }

}